$(function() {
	loadPlayer();
	ngl.init();
});

window.ngl = {
	init: function () { 
		
		this.addAll();
		this.clearPlaylist();
		this.makePlaylistSortable();
        $("#wrong-video").click(function (ev) {
            ev.preventDefault();
            var current = $('.playing');
            if (current.length != 0) {
                current = $(current.get(0));
                var id = current.attr('data-trackid');
                var link = current.find('a.bandname');
                var vids = link.attr('href');
                vids = vids.split('&');
                $.get('/wrongvideo/' + id + '/' + vids[0], function(data){
                    link.attr('href', data);
                    link.click();
                });
            }
        });
		$("#playlist, .songlist").disableSelection();
		this.showHidePlayer();

		$(".songlist .bandname").live('click', this.playme);
        $(".songlist").disableSelection();
        $(".delete-track").live('click', this.deleteTrack);
//        $("#playlist").droppable({
//            drop: function(ev, ui) {
//                ui.draggable.find(".label").each(function(elem) {
////                    angular.compile(elem)();
//                });
//
//            }
//        });
        $(".likebutton").live('click',  function(e) {
            e.preventDefault();
            var elem = $(this);
            elem.addClass('like-clicked');
            $.get('/like/' + elem.attr('data-id'), function() {
            });
        });
        $(".dislikebutton").live('click',  function(e) {
            e.preventDefault();
            var elem = $(this);
            elem.addClass('dislike-clicked');
            $.get('/dislike/' + elem.attr('data-id'), function() {
            });
        });

    }
};

ngl.addAll = function () {
	$('.add-all').live('click', function (ev) {
		ev.preventDefault();
		$('#playlist').append($(this).next().children().clone());
	});
};

ngl.makePlaylistSortable = function () {
	$("#playlist").sortable({
		revert: true,
    	stop: function(event, ui) {
			if(!ui.item.data('tag') && !ui.item.data('handle')) {
				ui.item.data('tag', true);
				//log(ui.item.index());
			}
    	},
    	receive: function (event, ui) {
    		//log(ui.item.index());
    	}
	});
};

ngl.playme = function (e) {
	e.preventDefault();
	me = $(e.target);
	if (me.parents("#playlist").length != 0) {
        if (!me.attr("href"))
            me = me.parent();
		var videoID = me.attr("href").split("&")[0];
        console.log(me.attr("href").split("&"));
		console.log(videoID);
		if(ytplayer) {
			ytplayer.loadVideoById(videoID);
		}
		$(".playing").removeClass("playing");
		me.parents('li.draggable').addClass("playing");
	} else {
		$("#playlist").append(me.parents('li.draggable').clone());
	}
};

ngl.clearPlaylist = function () {
	$('#playlist-clear').bind('click', function (ev) {
		ev.preventDefault();
		$('#playlist').empty();
		return false;
	});
};

ngl.deleteTrack = function (ev) {
	ev.preventDefault();
	var el = ev.target;
	$(el).parents('li.draggable').remove();
	return false;
};

ngl.showHidePlayer = function () {
	var yt_hidden;
	$('#show-hide-ytplayer').click(function() {
		if (ytplayer) {
			if (yt_hidden) {
				yt_hidden = false;
				$("#video-container").css("padding-bottom", "56.25%");
			}
			else {
				yt_hidden = true;
				$("#video-container").css("padding-bottom", "35px");
			}
		}
		return false; 
	});
};

function loadPlayer() {
	var videoID = "AA5DsLzSVrk";
	var params = { 	allowScriptAccess: "always", wmode: "opaque" };
	var atts = { id: "ytPlayer" };
	swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
	                   "?version=3&enablejsapi=1&playerapiid=player1",
	                   "videoDiv", "560", "360", "9", null, null, params, atts);
}

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("ytPlayer");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	// $('#video-container').fitVids();
}

function onytplayerStateChange(newState) {
	if (newState == 0) {
		var next = $(".playing");
		if (next) {
			if (next.next().length != 0) {
				next.next().find("a.bandname").click();
			} else {
				next.removeClass("playing");
			}
		}
	}
}
